import java.util.Random;
import java.util.Scanner;
import java.text.NumberFormat;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class 四则运算练习器 {
    public static void main(String[] args) throws ScriptException {
        char[] op = new char[]{'+', '-', '*', '/'};
        Random random = new Random();
        Scanner reader = new Scanner(System.in);
        NumberFormat num = NumberFormat.getPercentInstance();//调用getPercentInstance方法
        Judgement jud = new Judgement();
        FundCalculate f=new FundCalculate();
        int i;
        System.out.println("请输入要生成的题目数：");
        int n = reader.nextInt();
        System.out.println("请选择是否含有真分数计算：1——不含，2——含");
        int b=reader.nextInt();
        switch (b){
            case 1:for (i = 0; i < n; i++) {
                int a = random.nextInt(5) + 2;//2-5个运算符
                int[] number = new int[a + 1];//创建一个number数组
                String ex = new String();
                for (int j = 0; j <= a; j++) {//生产a+1个数字,即2-6个数字
                    number[j] = random.nextInt(100) + 1;//生成1-100的整数
                }
                int flag=random.nextInt(2);//随机控制有无括号
                int s[]=new int[4];
                for (int j = 0; j < a; j++) {
                    switch(flag){
                        case 0:
                            s[0]= random.nextInt(4);//随机生成运算符下标
                            ex += String.valueOf(number[j]) + String.valueOf(op[s[0]]);//添加一对数字和运算符
                            if(s[0]==3){number[j+1]=f.decide(number[j],number[j+1]);}
                            break;
                        case 1:
                            s[1]= random.nextInt(4);//随机生成运算符下标
                            ex+="("+String.valueOf(number[j]) + String.valueOf(op[s[1]]);//添加左括号和数字和运算符
                            if(s[1]==3){number[j+1]=f.decide(number[j],number[j+1]);}
                            flag++;
                            break;
                        case 2:
                            s[2]=random.nextInt(4);
                            ex+=String.valueOf(number[j]) + ")"+String.valueOf(op[s[2]]);//添加右括号和数字和运算符
                            if(s[2]==3){
                                switch(s[1]){//区分之前括号里运算结果对后面数取整
                                    case 0:
                                        number[j+1]=f.decide(number[j]+number[j-1],number[j+1]);
                                        break;
                                    case 1:
                                        number[j+1]=f.decide(number[j]-number[j-1],number[j+1]);
                                        break;
                                    case 2:
                                        number[j+1]=f.decide(number[j]*number[j-1],number[j+1]);
                                        break;
                                    case 3:
                                        number[j+1]=f.decide(number[j]/number[j-1],number[j+1]);
                                        break;
                                    default:;
                                }
                            }
                            flag=0;
                        default: ;
                    }
                }
                ex += String.valueOf(number[a]);//在尾端补上一个数字
                WAndRFile write=new WAndRFile();
                write.writeToFile(ex);
                System.out.println(ex + "=");

                int ua = reader.nextInt();//用户输入答案

                ScriptEngineManager manager = new ScriptEngineManager();
                ScriptEngine se1 = manager.getEngineByName("js");
                ScriptEngine se2 = manager.getEngineByName("js");
                String str=ex+"="+"="+ua;//将用户答案逻辑式放在字符串str中
                Boolean result =(Boolean)se1.eval(str);//用eval方法计算用户答案逻辑式
                String ca="="+se2.eval(ex);//生成correct_answer字符串存正确答案
                jud.judgeFormula(result,ca);
            }
                double T = (double) jud.getTrues()/n;
                System.out.println("正确率为："+num.format(T));
                break;
            case 2:
                Zhenfenshu z=new Zhenfenshu();
                z.makeAndCalculate(n);
                break;
        }
    }
}